﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Wicresoft.Shell.Data;
using Wicresoft.Shell.Logic;

namespace Wicresoft.Shell.Package.AppExp
{
    public partial class MyConfigurationControl : UserControl
    {
        private ConfigurationToolWindow parent;
        public string treeNodeID;
        private bool dataGridViewChanged = false;

        public bool DataGridViewChanged{ get { return this.dataGridViewChanged; } }

        public MyConfigurationControl(ConfigurationToolWindow parent)
        {
            InitializeComponent();
            this.dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
            this.parent = parent;
            BindData();
        }

        void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            dataGridViewChanged = true;
        }

        private void BindData()
        {
            if(null != GlobalData.myControl.selectedNode)
            {
                if (null != GlobalData.myControl.selectedNode.Tag)
                {
                    if (GlobalData.myControl.selectedNode.Tag.ToString().StartsWith("FID"))
                    {
                        int funID = Convert.ToInt32(GlobalData.myControl.selectedNode.Tag.ToString().Substring(3));
                        IEnumerable<FunctionProfTable> functionProf = Workspace.GetDatabaseLogic().GetFunctionProfTableDataByFuncID(funID);
                        DataTable dt = new DataTable();
                        dt.Columns.Add(Resources.PropertyName);
                        dt.Columns.Add(Resources.PropertyValue);
                        dt.Columns.Add("ColumnProfID");
                        foreach (FunctionProfTable row in functionProf)
                        {
                            if (row.FUNC_PROF_NAME != "INVOKE_TYPE" &&
                                row.FUNC_PROF_NAME != "DATA_TEMPLATE" &&
                                row.FUNC_PROF_NAME != "MOBILE_TEMPLATE" &&
                                row.FUNC_PROF_NAME != "CYCLE_DURATION" &&
                                !row.FUNC_PROF_VALUE.StartsWith("<"))
                            {
                                dt.Rows.Add(row.FUNC_PROF_NAME, row.FUNC_PROF_VALUE, row.FUNC_PROF_ID);
                            }
                        }
                        this.Column1.DataPropertyName = Resources.PropertyName;
                        this.Column2.DataPropertyName = Resources.PropertyValue;
                        this.ColumnProfID.DataPropertyName = "ColumnProfID";
                        this.dataGridView1.DataSource = dt;
                        this.parent.Caption = string.Format(Resources.ConfigurationTitle, GlobalData.myControl.selectedNode.Text);
                    }
                    else if (GlobalData.myControl.selectedNode.Tag.ToString().StartsWith("FPID"))
                    { }
                    else
                    {
                        int appID = Convert.ToInt32(GlobalData.myControl.selectedNode.Tag.ToString());
                        IEnumerable<ApplicationProfTable> applicationProf = Workspace.GetDatabaseLogic().GetApplicationProfTableDataByAppID(appID);
                        DataTable dt = new DataTable();
                        dt.Columns.Add(Resources.PropertyName);
                        dt.Columns.Add(Resources.PropertyValue);
                        dt.Columns.Add("ColumnProfID");
                        foreach (ApplicationProfTable row in applicationProf)
                        {
                            dt.Rows.Add(row.APP_PROF_NAME, row.APP_PROF_VALUE, row.APP_PROF_ID);
                        }
                        this.Column1.DataPropertyName = Resources.PropertyName;
                        this.Column2.DataPropertyName = Resources.PropertyValue;
                        this.ColumnProfID.DataPropertyName = "ColumnProfID";
                        this.dataGridView1.DataSource = dt;
                        this.parent.Caption = string.Format(Resources.ConfigurationTitle, GlobalData.myControl.selectedNode.Text);
 
                    }
                    this.treeNodeID = GlobalData.myControl.selectedNode.Tag.ToString();
                }
            }
        }

        public void UpdateData()
        {
            if(this.treeNodeID.StartsWith("FID"))
            {
                List<FunctionProfTable> functionProf_up = new List<FunctionProfTable>();
                List<FunctionProfTable> functionProf_add = new List<FunctionProfTable>();
                foreach (DataGridViewRow row in this.dataGridView1.Rows)
                {
                    //for the last row the cells[0] or cells[1] may be null
                    if (row.Cells[0].Value != null && row.Cells[1].Value != null)
                    {
                        FunctionProfTable data = new FunctionProfTable();
                        data.FUNC_PROF_NAME = row.Cells[0].Value.ToString();
                        data.FUNC_PROF_VALUE = row.Cells[1].Value.ToString();
                        data.FUNCTION_ID = Convert.ToInt32(this.treeNodeID.Substring(3));
                        if (!string.IsNullOrEmpty(row.Cells[2].Value.ToString()))
                        {
                            data.FUNC_PROF_ID = Convert.ToInt32(row.Cells[2].Value.ToString());
                            if(data.FUNC_PROF_NAME != string.Empty)
                                functionProf_up.Add(data);
                        }
                        else
                        {
                            data.FUNC_PROF_ID = 0;
                            if (data.FUNC_PROF_NAME != string.Empty)
                                functionProf_add.Add(data);
                        }
                    }
                }
                try
                {
                    Workspace.GetDatabaseLogic().UpdateFunctionProfTableData(functionProf_up);
                    Workspace.GetDatabaseLogic().InsertFunctionProfTableData(functionProf_add);
                    MessageBox.Show(Resources.UpdateSuccess, Resources.UpdatePropCaption, MessageBoxButtons.OK);
                    dataGridViewChanged = false;
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message, Resources.UpdatePropCaption, MessageBoxButtons.OK);
                }
            }
            else
            {
                List<ApplicationProfTable> applicationProf_up = new List<ApplicationProfTable>();
                List<ApplicationProfTable> applicationProf_add = new List<ApplicationProfTable>();
                foreach (DataGridViewRow row in this.dataGridView1.Rows)
                {
                    if (row.Cells[0].Value != null && row.Cells[1].Value != null)
                    {
                        ApplicationProfTable data = new ApplicationProfTable();
                        data.APP_PROF_NAME = row.Cells[0].Value.ToString();
                        data.APP_PROF_VALUE = row.Cells[1].Value.ToString();
                        data.APP_ID = Convert.ToInt32(this.treeNodeID);
                        if (!string.IsNullOrEmpty(row.Cells[2].Value.ToString()))
                        {
                            data.APP_PROF_ID = Convert.ToInt32(row.Cells[2].Value.ToString());
                            if(data.APP_PROF_NAME != string.Empty)
                                applicationProf_up.Add(data);
                        }
                        else
                        {
                            data.APP_PROF_ID = 0;
                            if (data.APP_PROF_NAME != string.Empty)
                                applicationProf_add.Add(data);
                        }
                    }
                }
                try
                {
                    int lastid = 0;
                    Workspace.GetDatabaseLogic().UpdateApplicationProfTableData(applicationProf_up);
                    Workspace.GetDatabaseLogic().InsertApplicationProfTableData(applicationProf_add, ref lastid);
                    MessageBox.Show(Resources.UpdateSuccess, Resources.UpdatePropCaption, MessageBoxButtons.OK);
                    dataGridViewChanged = false;
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message, Resources.UpdatePropCaption, MessageBoxButtons.OK);
                }
            }
        }
    }
}
